# !/usr/bin/python
# -*- coding: UTF-8 -*-
import pandas as pd
import calculate_salary


# 创建函数来从字典中获取信息
def get_info(pay, info_key):
    return info_dict.get(pay, {}).get(info_key, None)


# 定义全局pay字典
info_dict = {}


def get_all(pay):
    info_dict.setdefault(pay, calculate_salary.calc_day(pay))


def read():
    df = pd.read_excel('./salary.xlsx', sheet_name=0, header=0, skiprows=1)
    print('df.keys():', df.keys())
    # 删除所有是null的行
    df.dropna(axis="index", how='all', inplace=True)
    # df.dropna(axis="columns", how='all', inplace=True)

    # 将null设置为0
    df.loc[:, '保险'] = df['保险'].fillna(0)
    df.loc[:, '公积金'] = df['公积金'].fillna(0)
    df.loc[:, '个税'] = df['个税'].fillna(0)
    # 序号转int
    df.loc[:, '序号'] = df['序号'].astype('int32')

    # print(calculate_salary.month_work_day)

    df['应发合计'] = df['实发'] + df['保险'] + df['公积金'] + df['个税']
    df['保险'] = df['保险']
    df['公积金']
    df['个税']
    df['实发'] = df['实发']
    print(df.dtypes)
    print('-----------------------------')

    # 计算所有数据
    df['应发合计'].map(lambda x: get_all(x))

    # print('加载后数组长度info_dict 长度:', len(info_dict))

    df["时薪"] = df['应发合计'].map(lambda x: get_info(x, 'salary_hour'))
    df["银行卡号"] = df['应发合计'].map(lambda x: get_info(x, 'bank_card_no'))
    df["平时出勤"] = df['应发合计'].map(lambda x: get_info(x, 'work_day'))
    df["周末出勤"] = df['应发合计'].map(lambda x: get_info(x, 'work_weekend'))
    df["周六休息"] = df['应发合计'].map(lambda x: get_info(x, 'rest_weekend'))
    df["周六休息时间"] = df['应发合计'].map(lambda x: get_info(x, 'rest_weekend_detail'))
    df["请假天数"] = df['应发合计'].map(lambda x: get_info(x, 'takeOff'))
    df["请假周期"] = df['应发合计'].map(lambda x: get_info(x, 'takeOff_detail'))
    df["纸质版请假条"] = df['应发合计'].map(
        lambda x: get_info(x, 'is_leave_application'))
    df["正常:工时"] = df['应发合计'].map(lambda x: get_info(x, 'normal_work_time'))
    df["正常:工资"] = df['应发合计'].map(lambda x: get_info(x, 'normal_work_salary'))
    df["加班:工时"] = df['应发合计'].map(
        lambda x: get_info(x, 'workday_overtime_time'))
    df["加班:工资"] = df['应发合计'].map(
        lambda x: get_info(x, 'workday_overtime_salary'))
    df["周末:工时"] = df['应发合计'].map(
        lambda x: get_info(x, 'weekend_overtime_time'))
    df["周末:工资"] = df['应发合计'].map(
        lambda x: get_info(x, 'weekend_overtime_salary'))
    df["绩效"] = df['应发合计'].map(lambda x: get_info(x, 'extra'))

    # 列重新排序
    order = [
        '序号', '姓名', '时薪', '银行卡号', '平时出勤', '周末出勤', '周六休息', '周六休息时间', '请假天数',
        '请假周期', '纸质版请假条', '正常:工时', '正常:工资', '加班:工时', '加班:工资', '周末:工时', '周末:工资',
        '绩效', '应发合计', '保险', '公积金', '个税', '实发'
    ]
    df = df[order]
    print(df)

    df.to_excel("./salary_clean.xlsx", index=False)


# 保存生成好的excel 取消标题行
# df.to_excel("./datas/student_excel/student_excel_clean.xlsx", index=False)

if __name__ == '__main__':
    read()
